
/*
 * @(#)MaintFormGroupAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.form.dao.FormGroupDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractActionForm;

/**
  MaintFormGroupAction.java

  This class is for maint user role.

    @author          Tony Chen
    @company         DCIVision Limited
    @creation date   18/02/2004
    @version         $Revision: 1.3.32.2 $
*/

public class MaintFormGroupAction extends com.dcivision.setup.web.AbstractMaintPermissionAction {

  public static final String REVISION = "$Revision: 1.3.32.2 $";


  /** Creates a new instance of MaintFormGroupAction */
  public MaintFormGroupAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.form.dao.FormGroupDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.FORM_GROUP);
  }
  /**
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
      throws ServletException {

    AbstractActionForm maintForm = (AbstractActionForm) form;
    // Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward != null) {
      return forward;
    }
    if (GlobalConstant.OP_MODE_DELETE.equals(maintForm.getOpMode()) && this.isContainerForm(maintForm, request)) {

      addError(request, "errors.form.isContainerForm");
      maintForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
      return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
    }
    return super.execute(mapping, form, request, response);
  }
  /**
   * the mother return a value that use judge FormRecord,if container forms.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   */
  public boolean isContainerForm(AbstractActionForm form, HttpServletRequest request) {
    boolean result = false;
    try {
      AbstractDAObject dao = this.getMajorDAObject(request);
      FormGroupDAObject formdao = (FormGroupDAObject) dao;
      String groupIDArr[] = new String[]{form.getID()};
      int formCountID = formdao.getContainerFormCountByGroupID(groupIDArr);
      if (formCountID == 0) {
        result = false;
      } else {
        result = true;
      }
    } catch (ApplicationException e) {
      log.error(e);
    }
    return result;
  }
  /**
   * clearUniqueFields
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void clearUniqueFields(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintFormGroupForm formGroupForm = (MaintFormGroupForm)form;
    formGroupForm.setGroupName(null);
  }

  /**
   * getObjectType
   *
   * @return  The corresponding object type of permission setup action.
   */
  public String getObjectType() {
    return(GlobalConstant.OBJECT_TYPE_FORM);
  }

}
